package com.example.superoa.model.service;
  
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Mail;
import com.example.superoa.model.dao.PositionDao;
import com.example.superoa.model.dao.RapplyDao;
import com.example.superoa.model.dao.RbiographicalDao;
import com.example.superoa.model.dao.RinterviewDao;
import com.example.superoa.model.mappers.RbiographicalMapper;
import com.example.superoa.model.pojos.Position;
import com.example.superoa.model.pojos.Rapply;
import com.example.superoa.model.pojos.Rbiographical;
import com.example.superoa.model.pojos.Rinterview;

@Service
@Transactional(rollbackFor=Exception.class)
public class RbiographicalService {
		@Autowired
		private RbiographicalMapper biomapper;
		@Autowired
		private RbiographicalDao biodao;
		@Autowired 
		private PositionDao p_dao;
		@Autowired
		private RbiographicalMapper o_map;
		@Autowired
		private RinterviewDao r_dao;
		@Autowired
		private RapplyDao rapply_dao;
		
		Mail m=new Mail();
				
		//查询所有简历
		public JSONArray biofindAll(){
			return biomapper.biofindAll();
		}
		/**根据姓名 模糊查询简历
		 * @param name
		 * @return
		 */
		public JSONArray findByName(String reasyname){
			return biomapper.findByName("%"+reasyname+"%");
		}
		//新增简历
		public void insertrbiographical(Rbiographical biographical,Integer position_id,Integer rapplyid) {
			Position p=p_dao.findById(position_id).get();
			Rapply apply = rapply_dao.findById(rapplyid).get();
			List<Rbiographical> list=p.getRbiographicals();
			if(list==null) {
				list=new ArrayList();
			}
			list.add(biographical);
			biographical.setReasyid(biomapper.selectId()+1);
			biographical.setRapply(apply);
			biodao.save(biographical);
			p_dao.save(p);
		}		
		//根据id查看详情
		public JSONArray findByIdbio(int reasyid) {
			return biomapper.findByIdbio(reasyid);
		}
		//初始化
		public JSONArray selectallbio(int reasyid) {
			return biomapper.findByIdbio(reasyid);
		}
		//根据id修改计划面试时间
		public void xiugaibio(int reasyid, Date reasytime) {
			Rbiographical bio = biodao.findById(reasyid).get();
			bio.setReasytime(reasytime);
			biodao.save(bio);
		}
		//通知面试
		public Integer tongzhi(int id,int rid) {
			Rinterview view=r_dao.findById(id).get();
			JSONObject rb=(JSONObject)o_map.findByIdbio(rid).get(0);
			String str="";
			str+=rb.getString("reasyname");
			if(rb.getString("reasysex").equals("男")) {
				str+="先生";
			}else {
				str+="女士";
			} 
			str+="    请于"+view.getRiviewtime()+"来我公司进行面试";
			try {
				m.fasong(rb.getString("reasyemail"),str);
				return 1;
			} catch (Exception e) {
				return 2;
			}
		}
		//查询计划申请
		public JSONArray selectapply() {
			return biomapper.selectapply();
		}
}